package com.ssy.lingxi.contract.controller;

import com.ssy.lingxi.common.controller.BaseController;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.contract.model.vo.sign.request.*;
import com.ssy.lingxi.contract.model.vo.sign.response.ContractTemplateExampleResponse;
import com.ssy.lingxi.contract.model.vo.sign.response.ContractTemplateListResponse;
import com.ssy.lingxi.contract.model.vo.sign.response.ContractTemplateResponse;
import com.ssy.lingxi.contract.model.vo.sign.response.PreviewContractTemplateResponse;
import com.ssy.lingxi.contract.service.IContractTemplateService;
import com.ssy.lingxi.contract.utils.FileUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * @menu 合同能力 - 合同模板管理
 * @author yzc
 * @version 2.0.0
 * @date 2020/7/10
 */
@Slf4j
@RestController
@RequestMapping("/contract/contractTemplate")
public class ContractTemplateController extends BaseController {

    @Resource
    private IContractTemplateService contractTemplateService;

    /**
     * 合同模板分页列表
     * @param request 请求参数
     * @return 返回结果
     */
    @GetMapping("/page")
    public Wrapper<PageData<ContractTemplateListResponse>> pageContractTemplate(ContractTemplateListRequest request) {
        UserLoginCacheDTO sysUser = getSysUser();
        return contractTemplateService.pageContractTemplate(request, sysUser);
    }

    /**
     * 合同模板查询
     * @param request 请求参数
     * @return 返回结果
     */
    @GetMapping(value = "/get")
    public Wrapper<ContractTemplateResponse> getContractTemplate(ContractTemplateIdRequest request) {
        UserLoginCacheDTO sysUser = getSysUser();
        return contractTemplateService.getContractTemplate(request);
    }

    /**
     * 合同模板添加
     * @param request 请求参数
     * @return 返回结果
     */
    @PostMapping(value = "/add")
    public Wrapper<Void> addContractTemplate(@RequestBody @Validated ContractTemplateRequest request) {
        UserLoginCacheDTO sysUser = getSysUser();
        return contractTemplateService.addContractTemplate(request, sysUser);
    }

    /**
     * 合同模板修改
     * @param request 请求参数
     * @return 返回结果
     */
    @PostMapping(value = "/update")
    public Wrapper<Void> updateContractTemplate(@RequestBody @Validated ContractTemplateRequest request) {
        UserLoginCacheDTO sysUser = getSysUser();
        return contractTemplateService.updateContractTemplate(request, sysUser);
    }

    /**
     * 合同模板停用/启用
     * @param request 请求参数
     * @return 返回结果
     */
    @PostMapping(value = "/enable")
    public Wrapper<Void> updateStateContractTemplate(@RequestBody @Validated ContractTemplateStateRequest request) {
        UserLoginCacheDTO sysUser = getSysUser();
        return contractTemplateService.updateStatusContractTemplate(request, sysUser);
    }

    /**
     * 合同模板删除
     * @param request 请求参数
     * @return 返回结果
     */
    @PostMapping(value = "/delete")
    public Wrapper<Void> deleteContractTemplate(@RequestBody @Validated ContractTemplateIdRequest request) {
        UserLoginCacheDTO sysUser = getSysUser();
        return contractTemplateService.deleteContractTemplate(request, sysUser);
    }

    /**
     * 合同模板样例查询
     * @return 返回结果
     */
    @GetMapping(value = "/example")
    public Wrapper<ContractTemplateExampleResponse> getContractTemplateExample() {
        String exampleName = "合同模板样例.doc";
        String exampleUrl = "https://shushangyun-lingxi.oss-cn-shenzhen.aliyuncs.com/3450cb9f42e248dfb7d9cab7e55c1e041606903593087.doc";

        ContractTemplateExampleResponse response = new ContractTemplateExampleResponse();
        response.setContractName(exampleName);
        response.setContractUrl(exampleUrl);

        return Wrapper.success(response);
    }

    /**
     * 合同模板预览
     * @param request 请求参数
     * @param servletResponse
     * @return 返回结果
     */
    @GetMapping(value = "/preview")
    public Wrapper<PreviewContractTemplateResponse> previewContractTemplate(PreviewContractTemplateRequest request, HttpServletResponse servletResponse) {
        return contractTemplateService.previewContractTemplate(request);
    }

    /**
     * 合同文件下载
     * @param request 请求参数
     * @param servletResponse
     */
    @GetMapping(value = "/downloadContract")
    public void downloadContract(DownloadContractRequest request, HttpServletResponse servletResponse) {

        String fileName = request.getContractName();
        byte[] bytes = FileUtils.fileDownload(request.getContractUrl());

        try {
            servletResponse.setCharacterEncoding("UTF-8");
            servletResponse.setHeader("Content-Disposition", "attachment; filename=" + new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
            servletResponse.setHeader("Content-Length", String.valueOf(bytes.length));
            ServletOutputStream out = servletResponse.getOutputStream();
            out.write(bytes);
        } catch (IOException e) {
            log.error("合同文件下载失败", e);
        }
    }

}
